package xiaodaow.xiaodao360.com.previewphoto.preview;

import android.animation.Animator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;

import xiaodaow.xiaodao360.com.previewphoto.R;
import xiaodaow.xiaodao360.com.previewphoto.widget.photosview.PhotoView;
import xiaodaow.xiaodao360.com.previewphoto.widget.photosview.PhotoViewAttacher;


/**
 * 浏览视图类
 *
 * @author Baron
 */
public class PreviewPhoto {

    public static final long DURATION = 300;

    private Activity mActivity;
    private int mScreenWidth;
    private int mScreenHeight;
    private View mLayout;
    private Point currentPoint;

    private PhotoView mPhotoView;


    public static void startPreview(Activity activity, final ImageView thumb) {
        PreviewPhoto previewPhoto = new PreviewPhoto(activity);
        previewPhoto.ScreenFull(true);
        previewPhoto.showPreview(thumb);
    }

    public PreviewPhoto(Activity activity) {
        mActivity = activity;
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getMetrics(metrics);
        this.mScreenWidth = metrics.widthPixels;
        this.mScreenHeight = metrics.heightPixels;
    }


    /**
     * 建立View
     */
    private View createView(final ImageView thumb) {
        View view = LayoutInflater.from(mActivity).inflate(
                R.layout.preview, null);
        view.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hidePreview();
            }
        });
        int[] location = new int[2];
        thumb.getLocationOnScreen(location);
        ImageView imageView = (ImageView) view.findViewById(R.id.xi_preview);
        mPhotoView = (PhotoView) view.findViewById(R.id.xi_photoview);
        mPhotoView.setOnPhotoTapListener(getOnPhotoTapListener());
        imageView.setImageDrawable(thumb.getDrawable());
        imageView.setX(location[0]);
        imageView.setY(location[1]);

        Rect rect = new Rect(location[0], location[1], location[0]
                + thumb.getWidth(), location[1] + thumb.getHeight());
        slideview(imageView, rect);
        return view;
    }

    /**
     * 显示大图
     *
     * @param thumb
     */
    private void showPreview(final ImageView thumb) {
        mLayout = createView(thumb);
        getRootView(mActivity).addView(mLayout);
    }

    /**
     * 隐藏大图
     */
    private void hidePreview() {
        if (mLayout != null) {
            ScreenFull(false);
            getRootView(mActivity).removeView(mLayout);
        }
    }

    /**
     * 移动到屏幕中间动画
     */

    public void slideview(final ImageView imageView, Rect rect) {
        final float fromXDelta = rect.left;
        final float fromYDelta = rect.top;
        final int toXDelta = mScreenWidth / 2;
        final int toYDelta = mScreenHeight / 2;
        final float view_width = rect.width();
        final float view_height = rect.height();
        final float fromScaleX = 1;
        final float fromScaleY = 1;
        final float toScaleX = mScreenWidth
                / view_width;
        final float toScaleY = toScaleX;
        Point startPoint = new Point(fromXDelta, fromYDelta, fromScaleX,
                fromScaleY);
        Point endPoint = new Point(toXDelta - view_width / 2, toYDelta
                - view_height / 2, toScaleX, toScaleY);

        ValueAnimator xValue = ValueAnimator.ofObject(new PointEvaluator(),
                startPoint, endPoint);
        xValue.setDuration(DURATION);
        xValue.setInterpolator(new LinearInterpolator());// 线性效果变化
        xValue.addUpdateListener(new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentPoint = (Point) animation.getAnimatedValue();
                invalidate(imageView);
            }
        });
        xValue.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mPhotoView.setImageDrawable(imageView.getDrawable());
                imageView.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        xValue.start();
    }

    /**
     * 单击图片
     *
     * @return
     */
    public PhotoViewAttacher.OnPhotoTapListener getOnPhotoTapListener() {
        return new PhotoViewAttacher.OnPhotoTapListener() {

            @Override
            public void onPhotoTap(View view, float x, float y) {
                hidePreview();
            }
        };
    }

    public void invalidate(ImageView imageView) {
        if (currentPoint != null) {
            imageView.setX(currentPoint.getX());
            imageView.setY(currentPoint.getY());
            imageView.setScaleX(currentPoint.getScaleX());
            imageView.setScaleY(currentPoint.getScaleY());

        }
    }

    /**
     * 获取根视图
     *
     * @param activity
     * @return
     */
    private FrameLayout getRootView(Activity activity) {
        return (FrameLayout) (activity.getWindow().getDecorView()
                .findViewById(android.R.id.content));
    }

    /**
     * 屏幕是否全屏
     *
     * @param enable
     */
    private void ScreenFull(boolean enable) {
        if (enable) {
            WindowManager.LayoutParams lp = mActivity.getWindow()
                    .getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            mActivity.getWindow().setAttributes(lp);
            mActivity.getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            WindowManager.LayoutParams attr = mActivity.getWindow()
                    .getAttributes();
            attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mActivity.getWindow().setAttributes(attr);
            mActivity.getWindow().clearFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        }
    }

    public class PointEvaluator implements TypeEvaluator {

        @Override
        public Object evaluate(float fraction, Object startValue,
                               Object endValue) {
            Point startPoint = (Point) startValue;
            Point endPoint = (Point) endValue;
            float x = startPoint.getX() + fraction
                    * (endPoint.getX() - startPoint.getX());
            float y = startPoint.getY() + fraction
                    * (endPoint.getY() - startPoint.getY());
            float scaleX = startPoint.getScaleX() + fraction
                    * (endPoint.getScaleX() - startPoint.getScaleX());
            float scaleY = startPoint.getScaleY() + fraction
                    * (endPoint.getScaleY() - startPoint.getScaleY());
            Point point = new Point(x, y, scaleX, scaleY);
            return point;
        }

    }

    public class Point {
        private float x;
        private float y;
        private float scaleX;
        private float scaleY;

        public Point(float x, float y, float scaleX, float scaleY) {
            this.x = x;
            this.y = y;
            this.scaleX = scaleX;
            this.scaleY = scaleY;
        }

        public float getX() {
            return x;
        }

        public float getY() {
            return y;
        }

        public float getScaleX() {
            return scaleX;
        }

        public float getScaleY() {
            return scaleY;
        }
    }
}
